package com.lbw.controller;


import com.lbw.config.Comment;
import com.lbw.entity.QuestionEntity;
import com.lbw.entity.TestEntity;
import com.lbw.result.Result;
import com.lbw.result.ResultFactory;
import com.lbw.service.TestEntityService;
import com.lbw.vo.TestQuestionVo;
import com.lbw.vo.TestScoreVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 在线测试 前端控制器
 * </p>
 *
 * @author lbw
 * @since 2020-09-13
 */
@RestController
@RequestMapping("/testEntity")
@Api(tags = "在线测试")
public class TestEntityController {

    @Autowired
    private TestEntityService testEntityService;

    /**
     * 抽取在线测试题目，按科目测试
     *
     * @param testNum          测试编号
     * @param questionTableNum 题目表
     * @param subjectId        科目编号
     */
/*
    @PostMapping("/selectOnlineTest")
    @ApiOperation("抽取在线测试题目，按科目测试")
    public Result selectOnlineTest(@RequestParam("testNum") String testNum,
                                   @RequestParam("questionTableNum") String questionTableNum,
                                   @RequestParam("subjectId") String subjectId) {

        TestEntity testEntityList = testEntityService.selectOnlineTest(testNum, questionTableNum, subjectId);

        return ResultFactory.success(testEntityList);
    }
*/

    /**
     * 抽取在线测试题目，按章节测试
     *
     * @param testNum          测试编号
     * @param questionTableNum 题目表
     * @param type        科目编号
     * @param chapterId        章节编号
     */
    @PostMapping("/selectOnlineTestChapter")
    @ApiOperation("抽取在线测试题目，有章节id就按章节测试，无就按照科目测试")
    public Result selectOnlineTestChapter(@ApiParam("测试编号") @RequestParam("testNum") String testNum,
                                          @ApiParam("题目表编号：1为题目表，2为错题表") @RequestParam("questionTableNum") String questionTableNum,
                                          @ApiParam("科目类型（1为行测，2为申论）") @RequestParam("type") Integer type,
                                          @ApiParam("章节编号") @RequestParam(value = "chapterId",required = false) String chapterId) {

        TestEntity testEntityList = testEntityService.selectOnlineTestChapter(testNum, questionTableNum, type, chapterId);

        return ResultFactory.success(testEntityList);
    }

    /**
     * 在线抽取题目，抽取后存放在题目与测试中间表，新增题目
     */
    @PostMapping("/extractOnlineTestChapter")
    @ApiOperation("在线抽取题目，新增")
    public Result extractOnlineTestChapter(@RequestBody TestQuestionVo testQuestionVo) {


        boolean flag = testEntityService.extractOnlineTestChapter(testQuestionVo);
        if (flag) {
            return ResultFactory.success(true);
        }
        return ResultFactory.fail("出题失败");
    }

    /**
     * 展示测试列表，根据章节、科目
     */
    @PostMapping("/showTestList")
    @ApiOperation("展示测试列表，根据章节或科目")
    public Result showTestList(@ApiParam("科目类型（1为行测，2为申论）") @RequestParam("type") Integer type,
                               @ApiParam("章节") @RequestParam(value = "chapterId",required = false) String chapterId) {
        List<TestEntity> testEntityList = testEntityService.showTestList(type,chapterId);
        return ResultFactory.success(testEntityList);
    }

    /**
     * 统计测试分数以及测试错题，及错误答案
     */
    @PostMapping("/countTestScore")
    @ApiOperation("统计测试分数以及测试错题，及错误答案")
    public Result countTestScore(@RequestBody TestScoreVo testScoreVo) {
        boolean testEntityList = testEntityService.countTestScore(testScoreVo);
        if (testEntityList) {
            return ResultFactory.success(true);
        }
        return ResultFactory.fail("提交失败！");
    }


    /**
     * 如果为申论题目则从es中查询是否正确，返回给前端
     * 如果为行测题目，判断题目正确性，返回分值给前端
     */
    @PostMapping("/countCommentQuestion")
    @ApiOperation("判断题目正确性，返回分值")
    public Result countCommentQuestion(@ApiParam("题目id") @RequestParam("questionId") String questionId,
                                       @ApiParam("我的回答") @RequestParam("myAnswer") String myAnswer,
                                       @ApiParam("题目类型1为行测，2为申论") @RequestParam("type") Integer type,
                                       @ApiParam("学生id") @RequestParam("stuId") String stuId) throws IOException {
        QuestionEntity questionEntity = testEntityService.countCommentQuestion(questionId, myAnswer,type,stuId);

        return ResultFactory.success(questionEntity);
    }








}

